<?php
namespace app\index\controller;

use think\Controller;

class Index extends Controller
{
    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $account = db('account')->where('status', 1)->field('id, name, logo')->limit(0, 4)->order('id desc')->select();
        $this->assign('account', $account);
        return $this->fetch();
    }

    /**
     * 我的订单
     * @return mixed
     */
    public function myorder()
    {
        if (!session('?people_id')) {
            $this->redirect(url('login/index'));
        }

        $html = '';
        $myorder = db('putorder')->field('number, account, datepicker, position, status')->where('people_id', session('people_id'))->order('id desc')->select();
        foreach ($myorder as $vo) {
            switch ($vo['status']) {
                case 0:
                    $status_class = 'warning';
                    $status_txt = '待接单';
                    break;
                case 1:
                    $status_class = 'active';
                    $status_txt = '进行中';
                    break;
                case 2:
                    $status_class = 'info';
                    $status_txt = '已下单';
                    break;
                case 3:
                    $status_class = 'success';
                    $status_txt = '已完成';
                    break;
                case 4:
                    $status_class = 'danger';
                    $status_txt = '已撤销';
                    break;
                default:
                    $status_class = 'danger';
                    $status_txt = '未定义';
                    break;
            }

            $html = $html . '<tr class = "' . $status_class . '">' . '<td>' . $vo['number'] . '</td>' . '<td>' . $vo['account'] . '</td>' . '<td>' . $vo['datepicker'] . '</td>' . '<td>' . $vo['position'] . '</td>' . '<td>' . $status_txt . '</td></tr>';
        }

        $this->assign('myorder', $html);

        return $this->fetch();
    }

    /**
     * 投放广告
     * @return mixed
     */
    public function putad()
    {
        if (!input('?get.wid')) {
            $this->error('参数为空', 'index');
        }
        $account = db('account')->where('id', input('get.wid'))->find();

        $price0 = null;
        $html = '';
        foreach (json_decode($account['price'], true) as $value) {
            $html = $html . '<option price="' . $value['B'] . '" value="' . $value['A'] . '" exec_price="' . $value['C'] . '" deal_price="' . $value['D'] . '">' . $value['A'] . '</option>';
            if ($price0 == null) {
                $price0 = $value;
            }
        }

        $this->assign(['price0' => $price0,
            'html' => $html,
            'account' => $account]);

        return $this->fetch();
    }

    /**
     * 搜索
     * @return mixed
     */
    public function search()
    {
        if (request()->isPost()) {
            $wxtype = input('wxtype');
            $fanscount = input('fanscount');
            $area = input('area');

            $where = [];

            $where['status'] = ['eq', 1];

            if($wxtype != '不限') {
                $where['classified'] = ['like', '%' . $wxtype . '%'];
            }

            if($area != '不限') {
                $where['area'] = ['like', '%' . $area . '%'];
            }

            if($fanscount != '不限') {
                switch ($fanscount) {
                    case 0:
                        $where['fans'] = ['lt', 50000];
                        break;
                    case 1:
                        $where['fans'] = ['between', '50000, 100000'];
                        break;
                    case 2:
                        $where['fans'] = ['between', '100000, 500000'];
                        break;
                    case 3:
                        $where['fans'] = ['between', '500000, 1000000'];
                        break;
                    case 4:
                        $where['fans'] = ['gt', 1000000];
                        break;
                    default:
                        $where['fans'] = ['gt', 0];
                        break;
                }
            }

            $list = db('account')->field('id, name, fans, logo')->where($where)->select();
        } else {
            $list = db('account')->field('id, name, fans, logo')->select();
        }

        $this->assign('list', $list);

        return $this->fetch();
    }

    /**
     * 个人资料
     * @return mixed
     */
    public function user()
    {
        return $this->fetch();
    }

    /**
     * 公众号详情
     * @return mixed
     */
    public function detail()
    {
        if (!input('?get.wid')) {
            $this->error('参数为空', 'index');
        }
        $account = db('account')->where('id', input('get.wid'))->find();
        $this->assign('account', $account);

        return $this->fetch();
    }

    /**
     * 动作：下单
     */
    public function order()
    {
        $file = request()->file('pic');
        if($file) {
            $info = $file->move(ROOT_PATH . 'public/upload');
            if ($info) {
                // 成功上传后 获取上传信息
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $pic = $info->getSaveName();
            } else {
                // 上传失败获取错误信息
                $pic = '';
            }
        } else {
            $pic = '';
        }

        if(!input('contact') || !input('phone') || !input('datepicker')) {
            return json(['code' => 0, 'data' => '', 'msg' => '以下几项不可为空：联系人/联系电话/投放日期']);
        }

        $_POST['pic'] = $pic;
        $_POST['number'] = date("YmdHis");

        $ok = db('putorder')->insert($_POST);

        return json(['code' => $ok, 'data' => '', 'msg' => $ok == 1 ? '成功' : '失败']);
    }
}
